package cn.com.df;
import java.text.SimpleDateFormat;
import org.apache.log4j.Logger;
import smscommon.ComFun;
import smscommon.SmsClient;
import smsmessage.sms.DELIVER;
import smsmessage.sms.SMSclientSubmit;

/**
 * 短信下发
 * @author Administrator
 *
 */
public class MainSmsDemo1 {
	private static Logger logger = Logger.getLogger(MainSmsDemo1.class);

	public static void main(String args[]) throws Exception {

		SmsClient smsClient =SmsClient.getInstance();///建立socket连接
//		smsClient.pause();
		new SubmitSms5().start();
		new FindDeliverSms5().start();		

	} 
}
class SubmitSms5 extends Thread {
	private static Logger logger = Logger.getLogger(SubmitSms5.class);
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

	public void run() {
		for (int i = 0; i < 1; i++) {
			try {				
				SMSclientSubmit mt = new SMSclientSubmit();
				mt.setDestID("13636445902"); // 用户号码
				mt.setMsgContent("你好");
				mt.setPriority(1);//设置优先级，默认为1
				String retVal = "1";
				while ("1".equals(retVal) || "2".equals(retVal)
						|| "3".equals(retVal)) {
					Thread.sleep(1000);
					retVal = mt.sendSMS();	
					if ("1".equals(retVal)) {						
//						logger.error("下发缓冲池已满.");
					} else if ("2".equals(retVal)) {
//						logger.error("没有建立连接");
					} else if ("3".equals(retVal)) {
//						logger.error("流量超过设定值");
//						Thread.sleep(1000);
					}else{
						logger.info("企业下行消息标识 ["+retVal+"]，"
								+"短信内容 ["+mt.getMsgContent()+"]");						
					}
				}
			} catch (Exception e) {
				logger.error(this, e);
			}
		}
	}
}

class FindDeliverSms5 extends Thread {
	private static Logger logger = Logger.getLogger(FindDeliverSms5.class);

	public void run() {
		DELIVER deliver = new DELIVER();
		while (true) {
			if ((deliver = ComFun.getSMS()) != null) {
				if (deliver.getMsgFlag() == 1) {
					
					logger.info("-状态报告: " 
							+ "企业下行消息标识 ["	+ deliver.getMsgContent() + "]" 
							+ "短信接收情况描述 ["+ deliver.getReserve() + "]"); 
				} else {
					
					logger.info("-上行短信：" + "上行源号码 [" + deliver.getSrcID()
							+ "]+" + "上行目的号码 [" + deliver.getDestID() + "]+"
							+ "上行消息内容 [" + deliver.getMsgContent() + "]\n");
				}

			} else {
				try {
					Thread.sleep(1000);
				} catch (Exception e) {
					logger.error(e);
				}
			}
		}
	}
}
